#define HERE_EXEC
#include "userdef.h"
#define SEND_BYTE UART1_TxChar

void task_init(void)
{
	int i;
  for(i=0;i<SIZE_FUN;i++)
	{
		SuperFun[i].p_fun = NULL;
		SuperFun[i].nLine = 0;
		SuperFun[i].nStep = 0;
		SuperFun[i].bTick = 0;
		SuperFun[i].lock  = 0;
 }
}

uint8_t task_get(fun_ p_fun_name)
{
	int i;
	for(i=0;i<SIZE_FUN;i++)
	{
	  if (SuperFun[i].p_fun == p_fun_name) {
		  return 1;
	  }
	}

	for(i=0;i<SIZE_FUN;i++)
	{
	 if (SuperFun[i].p_fun == NULL){
		SuperFun[i].nLine = 0;
		SuperFun[i].nStep = 0;
		SuperFun[i].bTick = 0;
    SuperFun[i].lock  = 0;
		SuperFun[i].p_fun = p_fun_name;		
		break;
	 }
	}
	return 0;
}

void task_exec(void)
{
 uint8_t i;
 uint32_t xdata curr_tick,step;
 for(i=0;i<SIZE_FUN;i++)
 {
	if (SuperFun[i].p_fun != NULL && SuperFun[i].lock ==0){
		EA=0;
		curr_tick=sys_tick;
		EA=1;
		step =curr_tick-SuperFun[i].bTick;
		SuperFun[i].bTick =curr_tick;
		SuperFun[i].p_fun(i,step);
	}
 }
}

void task_del_other(fun_ p_fun_name)
{
	int i;
  for(i=0;i<SIZE_FUN;i++)
	{
  	if (SuperFun[i].p_fun == p_fun_name)continue;
		SuperFun[i].p_fun = NULL;
		SuperFun[i].nLine = 0;
		SuperFun[i].nStep = 0;
		SuperFun[i].bTick = 0;
    SuperFun[i].lock  = 0;
	}
}

void task_del(fun_ p_fun_name)
{
 int i;
 for(i=0;i<SIZE_FUN;i++)
 {
		if(SuperFun[i].p_fun == p_fun_name)
		{
			SuperFun[i].nLine = 0;
			SuperFun[i].nStep = 0;
			SuperFun[i].p_fun = NULL;
			SuperFun[i].bTick = 0;
			SuperFun[i].lock  = 0;
			break;
		}
 }
}

void debug_i32(int32_t i)
{
	int32_t xdata j = 1000000000;
	uint8_t byte;
	if (i < 0) byte='-',SEND_BYTE(byte), i = -i;
	while (j >= 10) {
		if (i >= j) break;
		j /= 10;
	}
	while (j >= 10) {
		byte='0'+i/j;
		SEND_BYTE(byte);
		i %= j;
		j /= 10;
	}
	byte='0'+i;
	SEND_BYTE(byte);
}

void debug_u32(uint32_t i)
{
	uint32_t xdata j = 1000000000;
	uint8_t byte;
	while (j >= 10) {
		if (i >= j) break;
		j /= 10;
	}
	
	while (j >= 10) {
		byte='0'+i/j;
		SEND_BYTE(byte);
		i %= j;
		j /= 10;
	}
	byte='0'+i;
	SEND_BYTE(byte);
}

void Delay1us()//@35MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 9;
	while (--i);
}

void Delay1ms()//@35MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 46;
	j = 113;
	do
	{
		while (--j);
	} while (--i);
}

void delay_nus(uint16_t nus)
{
	do{
		Delay1us();
	}while(--nus);
}

void delay_nms(uint16_t nus)
{
	do{
		Delay1ms();
	}while(--nus);
}


